import {Page} from '@/base/Page';
import {DeviceMaintainRule} from '@/base/model/DeviceMaintainRule';
import {getDeviceMaintainList, putDeviceMaintain} from '@/base/api/DeviceMaintainApi';
import {deepAssign} from '@/base/util/Objects';
import {getDeviceSort} from '@/base/api/DeviceSortApi';
import {DeviceSort} from '@/base/model/DeviceSort';
import {getDeviceModel} from '@/base/api/DeviceModelApi';
import {DeviceModel} from '@/base/model/DeviceModel';
import {getSeftMotion} from '@/base/api/SeftMotionApi';
import {SeftMotion} from '@/base/model/SeftMotion';
import {getEvaluationList} from '@/base/api/EvaluationApi';
import {Evaluation} from '@/base/model/Evaluation';
import {isEmpty} from '@/base/util/Strings';

export class FormBloc {
  model: any = {
    modelId: 0 || undefined,
    modelName: '',
    sortId: 0 || undefined,
    sortName: '',
    selfMotionId: 0 || undefined,
    modeName: '',
    phone: '',
    date: [],
    createBegin: '',
    createEnd: ''
  };
  paged: Page<Evaluation> = new Page<Evaluation>([], 0, 1, 15, 0, []);
  sortpage: Page<DeviceSort> = new Page<DeviceSort>([], 0, 1, 10, 0, []);
  modelpage: Page<DeviceModel> = new Page<DeviceModel>([], 0, 1, 10, 0, []);
  modepage:Page<SeftMotion> = new Page<SeftMotion>([], 0, 1, 10, 0, []);
  sortList: any = [];
  modelList: any = [];
  modeList: any = [];
  loading = false;

  load(pageIndex: number, pageSize: number) {
    this.loading = true;
    this.model.modelName = this.getmodelName(this.model.modelId);
    this.model.sortName = this.getSortName(this.model.sortId);
    this.model.modeName = this.getmodeName(this.model.selfMotionId);

    if (!isEmpty(this.model.date) && !isEmpty(String(this.model.date[0]))) {
      // @ts-ignore
      this.model.createBegin = this.model.date[0].getTime() || '';
      // @ts-ignore
      this.model.createEnd = this.model.date[1].getTime() + 24 * 60 * 60 * 1000 || '';
    } else {
      this.model.createBegin = '';
      this.model.createEnd = '';
    }
    return getEvaluationList({
      pageIndex,
      pageSize,
      sortDirection: 'DESC',
      createBegin: this.model.createBegin,
      createEnd: this.model.createEnd,
      phone: this.model.phone,
      sortName: this.model.sortName,
      modelName: this.model.modelName,
      modeName: this.model.modeName
    }).then((res) => {
      // console.log(res.data);
      this.paged = res.data;
    }).finally(() => {
      this.loading = false;
    });
  }
  reset() {
    this.model = {
      modelId: 0 || undefined,
      modelName: '',
      sortId: 0 || undefined,
      sortName: '',
      selfMotionId: 0 || undefined,
      modeName: '',
      phone: '',
      date: [],
      createBegin: '',
      createEnd: ''
    }; // 重置要把表单还原
    this.paged.pageIndex = 1;
    this.paged.pageSize = 15;
    this.load(this.paged.pageIndex, this.paged.pageSize);
    this.getAllSort(1, 1000, 'DESC');
    this.getAllModel(1, 1000, undefined, 'DESC', undefined);
    this.getAllMode(1, 1000, undefined, 'DESC')
  }

  getsortName(sortId: number) {
    const item = this.sortList.find(x => x.id === sortId);
    return item == null ? '' : item.sortName;
  }
  getmodeName(selfMotionId: number) {
    const item = this.modeList.find(x => x.id === selfMotionId);
    return item == null ? '' : item.modeName;
  }
  getmodelId(modelName: string) {
    const item = this.modelList.find(x => x.modelName === modelName);
    return item == null ? '' : item.id;
  }

  getAllSort(pageIndex: number, pageSize: number, sortDirection: string) {
    this.loading = true;
    let that = this;
    return getDeviceSort({
      pageIndex,
      pageSize,
      sortDirection
    }).then((res) => {
      deepAssign(this.sortpage, res.data);
      this.sortList = this.sortpage.items.map(function (item) {
        var obj = { id: 0, sortName: '' };
        // @ts-ignore
        obj.id = item.id;
        obj.sortName = item.sortName;
        return obj;
      });
      return this.sortList;
    }).finally(() => {
      this.loading = false;
    });
  }

  getSortName(id) {
    let item = this.sortList.find(item => item.id === id);
    return item == null ? '' : item.sortName;
  }
  getmodelName(modelId: number) {
    const item = this.modelList.find(x => x.id === modelId);
    return item == null ? '' : item.modelName;
  }
  getAllModel(pageIndex: number, pageSize: number, sortId, sortDirection: string, companyId) {
    this.loading = true;
    let that = this;
    return getDeviceModel({
      pageIndex,
      pageSize,
      sortId,
      sortDirection,
      companyId
      // id: this.model.id,
      // modelName: this.model.modeName
    }).then((res) => {
      deepAssign(this.modelpage, res.data);
      this.modelList = this.modelpage.items.map(function (item) {
        var obj = { id: 0, modelName: '' };
        obj.id = item.id;
        obj.modelName = item.modelName;
        return obj;
      });
      return this.modelList;
    }).finally(() => {
      this.loading = false;
    });
  }
  getAllMode(pageIndex: number, pageSize: number, modelId, sortDirection: string) {
    this.loading = true;
    let that = this;
    return getSeftMotion({
      pageIndex,
      pageSize,
      sortDirection,
      modelId
    }).then((res) => {
      deepAssign(this.modepage, res.data);
      this.modeList = this.modepage.items.map(function (item) {
        var obj = { id: 0, modeName: '' };
        obj.id = item.id;
        obj.modeName = item.modeName;
        return obj;
      });
      return this.modeList;
    }).finally(() => {
      this.loading = false;
    });
  }
  shuchu(value) {
    let rating = '';
    for (let i = 0; i < value; i++) {
      rating += '★';
    }
    for (let a = 0; a < 5 - value; a++) {
      rating += '☆';
    }
    return rating;
  }
}
